﻿using System;
using System.IO;
using Sherwood.Configuration;
using Microsoft.Practices.Unity;
using Sherwood.Imaging;
using Sherwood.Imaging.Behaviors;
using Sherwood.Imaging.Filters;
using Sherwood.Imaging.Providers;

namespace Sherwood.Samples.ContentServer
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            var pluginProvider = DependencyInjection.Resolve<IPluginProvider>();
            pluginProvider.InstantiatePlugins<object>("Initializers", true);

            SetupImageTemplateContainer();
        }

        private static void SetupImageTemplateContainer()
        {
            Template.DefaultImageProvider = new UrlImageProvider(new VirtualPathSegmentProvider(2), "FileStore");
            var container = new TemplateContainer();

            var secondUrlSegment = new VirtualPathSegmentProvider(1, 1)
                                       {
                                           RemoveFirstSegmentSeparator = new StaticBooleanProvider(true)
                                       };

            var resizeAndClipFilter = new ScaleImageFilter(
                new StringToNumberProvider(new SplitStringProvider(secondUrlSegment, "x", 0)),
                new StringToNumberProvider(new SplitStringProvider(secondUrlSegment, "x", 1)),
                new SizeAndClipBehavior(true, true));
            container.RegisterTemplate(new Template("resizeAndClip") {Filters = {resizeAndClipFilter}});


           var resizeFilter =
                new ScaleImageFilter(
                    new StringToNumberProvider(new SplitStringProvider(secondUrlSegment, "x", 0)),
                    new StringToNumberProvider(new SplitStringProvider(secondUrlSegment, "x", 1)),
                    new SimpleResizeBehavior());
            container.RegisterTemplate(new Template("resize") {Filters = {resizeFilter}});

            DependencyInjection.Container.RegisterInstance<ITemplateContainer>(container);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}